package com.leyou.auth.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;
import com.leyou.auth.config.OSSProperties;
import com.leyou.auth.dto.AliOssSignatureDTO;
import com.leyou.auth.service.AliAnthService;
import com.leyou.common.exceptions.LyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.sql.Date;

@Service
public class AliAuthServiceImpl implements AliAnthService {

    @Autowired
    private OSS oss;
    @Autowired
    private OSSProperties properties;

    @Override
    public AliOssSignatureDTO getAliSignature() {
//        String accessId = "<yourAccessKeyId>"; // 请填写您的AccessKeyId。
//        String accessKey = "<yourAccessKeySecret>"; // 请填写您的AccessKeySecret。
//        String endpoint = "oss-cn-hangzhou.aliyuncs.com"; // 请填写您的 endpoint。
//        String bucket = "bucket-name"; // 请填写您的 bucketname 。
//        String host = "http://" + bucket + "." + endpoint; // host的格式为 bucketname.endpoint
//        // callbackUrl为 上传回调服务器的URL，请将下面的IP和Port配置为您自己的真实信息。
//        String callbackUrl = "http://88.88.88.88.:8888";
//        String dir = "user-dir-prefix/"; // 用户上传文件时指定的前缀。

        //OSSClient client = new OSSClient(endpoint, accessId, accessKey);
        try {
            //有效时间
            long expireTime = properties.getExpireTime();
            //到期结束的时间
            long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
            Date expiration = new Date(expireEndTime);
            //文件上传限制
            PolicyConditions policyConds = new PolicyConditions();
            //文件大小限制
            policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, properties.getMaxFileSize());
            //文件上传位置限制
            policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, properties.getDir());

            String postPolicy = oss.generatePostPolicy(expiration, policyConds);
            byte[] binaryData = postPolicy.getBytes("utf-8");
            String encodedPolicy = BinaryUtil.toBase64String(binaryData);
            //获取签名
            String postSignature = oss.calculatePostSignature(postPolicy);
            System.out.println("postSignature = " + postSignature);

//            Map<String, String> respMap = new LinkedHashMap<String, String>();
//            respMap.put("accessid", accessId);
//            respMap.put("policy", encodedPolicy);
//            respMap.put("signature", postSignature);
//            respMap.put("dir", dir);
//            respMap.put("host", host);
//            respMap.put("expire", String.valueOf(expireEndTime / 1000));

            return AliOssSignatureDTO.of(properties.getAccessKeyId(),properties.getHost(),encodedPolicy,postSignature,expireEndTime,properties.getDir());

        } catch (Exception e) {
            // Assert.fail(e.getMessage());
//            System.out.println(e.getMessage());
            throw new LyException(500, "签名授权异常", e);
        }
    }
}
